//
//  GroupDetailViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "GroupDetailViewController.h"
#import "MainDataHelper.h"
#import "TopicModel.h"
#import "GroupDetailFirstTableViewCell.h"
#import "GroupDetailSecondTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "GroupDetail2ViewController.h"

@interface GroupDetailViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, copy) NSString *string;
@property (nonatomic, copy) NSString *bodyString;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation GroupDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self loadTableView];
}

#pragma mark --- 数据解析 ---
- (void)loadData
{
    self.dataArray = [NSMutableArray array];
    
    self.string = @"http://api2.pianke.me/group/group_info";
    self.bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&groupid=%@&version=3.0.6",self.groupid];
    
    [MainDataHelper postDataWithUrlString:self.string andBodyString:self.bodyString compare:^(id object) {
        NSDictionary *dic = object;
        NSArray *array = dic[@"data"][@"postslist"];
        
        for (NSDictionary *dic1 in array) {
            TopicModel *model = [[TopicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataArray addObject:model];
        }
        
        [self.tableView reloadData];
    }];
}

#pragma mark --- tableView部分 ---
- (void)loadTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

#pragma mark --- cell赋值部分 ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicModel *model = self.dataArray[indexPath.row];
    
    if ([model.coverimg isEqualToString:@""]) {
        UINib *nib = [UINib nibWithNibName:@"GroupDetailFirstTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:@"groupFirstCell"];
        GroupDetailFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupFirstCell" forIndexPath:indexPath];
        
        cell.titleLabel.text = model.title;
        cell.contentLabel.text = model.content;
        cell.contentLabel.numberOfLines = 0;
        [cell.contentLabel sizeToFit];
        cell.timeLabel.text = model.addtime_f;        cell.joinLabel.text = [NSString stringWithFormat:@"%@", model.counterList[@"comment"]];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else {
        UINib *nib = [UINib nibWithNibName:@"GroupDetailSecondTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:@"groupSecondCell"];
        GroupDetailSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"groupSecondCell" forIndexPath:indexPath];
        
        cell.titleLabel.text = model.title;
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
        cell.contentLabel.text = model.content;
        cell.contentLabel.numberOfLines = 0;
        [cell.contentLabel sizeToFit];
        cell.timeLabel.text = model.addtime_f;
        cell.joinLabel.text = [NSString stringWithFormat:@"%@", model.counterList[@"comment"]];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicModel *model = self.dataArray[indexPath.row];
    if ([model.coverimg isEqualToString:@""]) {
        return 250;
    }else {
        return 220;
    }
    
}

#pragma mark --- cell点击跳转页面 ---
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicModel *model = self.dataArray[indexPath.row];
    GroupDetail2ViewController *group2VC = [[GroupDetail2ViewController alloc] init];
    group2VC.contentid = model.contentid;
    [self.navigationController pushViewController:group2VC animated:YES];
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
